<?php
//项目编号函数
use think\facade\Config;

function ProjectNo($no){
    $pex = Config::get('app.project_name');
    return $pex.$no;
}
/**
 *   将数组转换为xml(供esb使用)
 *    @param array $data    要转换的数组
 *   @param bool $root     是否要根节点
 *   @return string         xml字符串
 *    @author Dragondean
 *    @url    http://www.cnblogs.com/dragondean
 */
function arr2xml($data, $root = true){
    $str="";
    if($root)$str .= "<?xml version='1.0' encoding='utf-8'?>";
    foreach($data as $key => $val){
        if(is_array($val)){
            $child = arr2xml($val, false);
            $str .= "<$key>$child</$key>";
        }else{
            $str.= "<$key>$val</$key>";
        }
    }
    return $str;
}

// Xml 转 数组, 不包括根键
function xmltoarray( $xml )
{
    $arr = xml_to_array($xml);
    $key = array_keys($arr);
    return $arr[$key[0]];
}

function xml_to_array( $xml )
{
    $reg = "/<(\\w+)[^>]*?>([\\x00-\\xFF]*?)<\\/\\1>/";
    if(preg_match_all($reg, $xml, $matches))
    {
        $count = count($matches[0]);
        $arr = array();
        for($i = 0; $i < $count; $i++)
        {
            $key= $matches[1][$i];
            $val = xml_to_array( $matches[2][$i] );  // 递归
            if(array_key_exists($key, $arr))
            {
                if(is_array($arr[$key]))
                {
                    if(!array_key_exists(0,$arr[$key]))
                    {
                        $arr[$key] = array($arr[$key]);
                    }
                }else{
                    $arr[$key] = array($arr[$key]);
                }
                $arr[$key][] = $val;
            }else{
                $arr[$key] = $val;
            }
        }
        return $arr;
    }else{
        return $xml;
    }
}

/**
 * 二维数组根据某个字段排序
 * @param array $array 要排序的数组
 * @param string $keys   要排序的键字段
 * @param string $sort  排序类型  SORT_ASC     SORT_DESC
 * @return array 排序后的数组
 */
function arraySort($array, $keys, $sort = SORT_DESC) {
    $keysValue = [];
    foreach ($array as $k => $v) {
        $keysValue[$k] = $v[$keys];
    }
    array_multisort($keysValue, $sort, $array);
    return $array;
}

//post XML
function http_request_xml($url,$data = null,$arr_header = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    // curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    if(!empty($arr_header)){
        curl_setopt($curl, CURLOPT_HTTPHEADER, $arr_header);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

//post base Auth
function http_request_base_auth($url,$data = null,$username,$password){

        $curl = curl_init();

        curl_setopt_array($curl, array(
            //CURLOPT_PORT => "9001",
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $data,
            //CURLOPT_POSTFIELDS => "{\r\n \"REQUEST\": {\r\n  \"HEAD\": {\r\n   \"BIZTRANSACTIONID\": \"abn\",\r\n   \"COUNT\": \"string\",\r\n   \"CONSUMER\": \"string\",\r\n   \"SRVLEVEL\": \"string\",\r\n   \"ACCOUNT\": \"test123\",\r\n   \"PASSWORD\": \"test123456\",\r\n   \"PAGENUM\": \"string\",\r\n   \"PAGESIZE\": \"string\",\r\n   \"MODULE\": \"string\"\r\n  },\r\n  \"LIST\": [{\r\n   \"pub\": \"XT-45ad439f-ce77-488a-91c2-6b0bc63dc4b6\",\r\n   \"pubtoken\": \"f6511a847dcc3a90b730a31d86da365a\",\r\n   \"receivers\": [\r\n   {\r\n\t\t\t\t\t \"telphone\": \"15330518878\"\r\n\t\t\t\t\t}\r\n   ],\r\n   \"text\": [{\r\n     \"context\": \"123\"\r\n    },\r\n    {\r\n     \"context\": \"123\"\r\n    }\r\n   ],\r\n   \"Title\": \"消息标题\",\r\n   \"sendType\": \"url\"\r\n  }]\r\n }\r\n}",
            CURLOPT_HTTPHEADER => array(
                "authorization: Basic dGVzdDEyMzp0ZXN0MTIzNDU2",
                "cache-control: no-cache",
                "content-type: application/json",
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            return "cURL Error #:" . $err;
        } else {
            return $response;
        }

}

function uuid($prefix=''){
    $chars = md5(uniqid(mt_rand(), true));
    $uuid  = substr($chars,0,8) . '-';
    $uuid .= substr($chars,8,4) . '-';
    $uuid .= substr($chars,12,4) . '-';
    $uuid .= substr($chars,16,4) . '-';
    $uuid .= substr($chars,20,12);
    return $prefix . $uuid;
}

function wlog($name,$str){
    $file_tmp ="../log/".$name.".txt";
    $myfile = fopen($file_tmp, "w") or die("Unable to open file!");
    $txt = json_encode($str,true);
    fwrite($myfile, $txt);
    fclose($myfile);
}

/**
 * 校验日期格式是否合法
 * @param string $date
 * @param array $formats
 * @return bool
 */
function isDateValid($date, $formats = array('Y-m-d', 'Y/m/d')) {

    $unixTime = strtotime($date);
    if(!$unixTime) { //无法用strtotime转换，说明日期格式非法
        return false;
    }

    //校验日期合法性，只要满足其中一个格式就可以
    foreach ($formats as $format) {
        if(date($format, $unixTime) == $date) {
            return true;
        }
    }

    return false;
}

/**
 * 二维数组去重复
 * @param $array
 * @return array
 */
function a_array_unique($array)//写的比较好
{
    $out = array();
    foreach ($array as $key=>$value) {
        if (!in_array($value, $out))
        {
            $out[$key] = $value;
        }
    }
    return $out;
}